<?php

namespace app\admin\services;

use app\admin\model\StoreModel;

class StoreService extends BaseService
{
    public function list($param = [], $field = "*")
    {
        $storeModel = StoreModel::makeInstance();
        $list = $storeModel->list($param, $field);
        return $list;
    }

    public function save($param = [], $id = 0)
    {
        $storeModel = StoreModel::makeInstance();
        if ($id == 0) {
            $param['create_time'] = time();
            $param['update_time'] = time();
            $param['create_id'] = session('aid');
            $param['update_id'] = session('aid');
            $res = $storeModel->mInsert($param);
        } else {
            $param['update_time'] = time();
            $param['update_id'] = session('aid');
            $res = $storeModel->mUpdate($param, $id);
        }
        return $res;
    }

    public function getInfoById($id, $param = [])
    {
        $storeModel = StoreModel::makeInstance();
        return $storeModel->getInfoById($id, $param);
    }

    /**
     * 获取有效数据
     */
    public function getOpenData($field = "*")
    {
        $storeModel = StoreModel::makeInstance();
        $list = $storeModel->getDatasByWhere([
            'is_delete' => 1,
            'status' => 1
        ], $field)->toArray();
        return array_column($list, null, 'id');
    }

    /**
     * 根据ids获取数据
     * @param array $ids
     * @param string $field
     * @return array
     */
    public function getDatasByIds($ids = [], $field = "*")
    {
        $storeModel = StoreModel::makeInstance();
        $list = $storeModel->getDatasByIds($ids, $field)->toArray();
        return array_column($list, null, 'id');
    }

    /**
     * 获取所有数据
     */
    public function getAllData($field = [])
    {
        $storeModel = StoreModel::makeInstance();
        $list = $storeModel->getDatasByWhere([
            'status' => 1
        ], $field)->toArray();
        return array_column($list, null, 'id');
    }

    /**
     * 根据关键字获取店铺列表
     * @param string $keyword
     */
    public function getListByTitle($keyword = "", $field = [])
    {
        $storeModel = StoreModel::makeInstance();
        if (isset($keyword) && !empty($keyword)) {
            $where[] = ['title', 'like', "%{$keyword}%"];
        }
        $where[] = ['is_delete', 'eq', 1];
        $where[] = ['status', 'eq', 1];
        $list = $storeModel->where($where)->field($field)->select();
        return $list;
    }

}